package com.wpen.unit.infra.repository.job.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.wpen.unit.exception.UpdateZeroException;
import com.wpen.unit.infra.mybatis.job.mapper.JobInfoMapper;
import com.wpen.unit.infra.mybatis.job.po.JobInfoPo;
import com.wpen.unit.infra.mybatis.job.po.JobInfoPo.JobInfoColumns;
import com.wpen.unit.infra.repository.job.JobInfoRepository;
import com.wpen.unit.utils.StringUtil;

/**
 * 任务作业维护.
 * 
 * @author pg
 *
 */
@Repository
@Transactional
public class JobInfoRepositoryImpl extends ServiceImpl<JobInfoMapper, JobInfoPo> implements JobInfoRepository {

    @Override
    public List<JobInfoPo> queryListByTask(String bizCode, String batchCode, String taskCode) {
        QueryWrapper<JobInfoPo> queryWrapper = Wrappers.query();
        queryWrapper.eq(JobInfoColumns.BIZ_CODE.name(), bizCode);
        queryWrapper.eq(JobInfoColumns.BATCH_CODE.name(), batchCode);
        queryWrapper.eq(JobInfoColumns.TASK_CODE.name(), taskCode);
        return getBaseMapper().selectList(queryWrapper);
    }

    @Override
    public JobInfoPo queryByCode(String bizCode, String batchCode, String taskCode, String jobCode) {
        QueryWrapper<JobInfoPo> queryWrapper = Wrappers.query();
        queryWrapper.eq(JobInfoColumns.BIZ_CODE.name(), bizCode);
        queryWrapper.eq(JobInfoColumns.BATCH_CODE.name(), batchCode);
        queryWrapper.eq(JobInfoColumns.TASK_CODE.name(), taskCode);
        queryWrapper.eq(JobInfoColumns.JOB_CODE.name(), jobCode);
        return super.getOne(queryWrapper);
    }

    @Override
    public boolean deleteByCode(String bizCode, String batchCode, String taskCode) {
        UpdateWrapper<JobInfoPo> updateWrapper = Wrappers.update();
        updateWrapper.eq(JobInfoColumns.BIZ_CODE.name(), bizCode);
        updateWrapper.eq(JobInfoColumns.BATCH_CODE.name(), batchCode);
        updateWrapper.eq(JobInfoColumns.TASK_CODE.name(), taskCode);
        return super.remove(updateWrapper);
    }

    @Override
    public long count(JobInfoPo jobInfoPo) {
        QueryWrapper<JobInfoPo> queryWrapper = createQuery(jobInfoPo);
        return super.count(queryWrapper);
    }

    @Override
    public List<JobInfoPo> queryList(int index, int size, JobInfoPo jobInfoPo) {
        QueryWrapper<JobInfoPo> queryWrapper = createQuery(jobInfoPo);
        if (size > 0) {
            Page<JobInfoPo> page = new Page<>(index, size, false);
            return super.page(page, queryWrapper).getRecords();
        }
        // queryWrapper.orderBy(true, false, BatchInfoPoColumns.ID.name());
        return getBaseMapper().selectList(queryWrapper);
    }

    private QueryWrapper<JobInfoPo> createQuery(JobInfoPo jobInfoPo) {
        QueryWrapper<JobInfoPo> queryWrapper = Wrappers.query();
        if (jobInfoPo.getId() != null) {
            queryWrapper.eq(JobInfoColumns.ID.name(), jobInfoPo.getId());
        }
        if (StringUtil.isNotEmpty(jobInfoPo.getBizCode())) {
            queryWrapper.eq(JobInfoColumns.BIZ_CODE.name(), jobInfoPo.getBizCode());
        }
        if (StringUtil.isNotEmpty(jobInfoPo.getBatchCode())) {
            queryWrapper.eq(JobInfoColumns.BATCH_CODE.name(), jobInfoPo.getBatchCode());
        }
        if (StringUtil.isNotEmpty(jobInfoPo.getTaskCode())) {
            queryWrapper.eq(JobInfoColumns.TASK_CODE.name(), jobInfoPo.getTaskCode());
        }
        if (StringUtil.isNotEmpty(jobInfoPo.getJobCode())) {
            queryWrapper.eq(JobInfoColumns.JOB_CODE.name(), jobInfoPo.getJobCode());
        }
        // if (jobInfoPo.getOrders() != null) {
        // queryWrapper.eq(JobInfoColumns.ORDERS.name(), jobInfoPo.getOrders());
        // }
        if (StringUtil.isNotEmpty(jobInfoPo.getEnabled())) {
            queryWrapper.eq(JobInfoColumns.ENABLED.name(), jobInfoPo.getEnabled());
        }
        // if (StringUtil.isNotEmpty(jobInfoPo.getJobDesc())) {
        // queryWrapper.eq(JobInfoColumns.JOB_DESC.name(), jobInfoPo.getJobDesc());
        // }
        if (StringUtil.isNotEmpty(jobInfoPo.getFailIgnore())) {
            queryWrapper.eq(JobInfoColumns.FAIL_IGNORE.name(), jobInfoPo.getFailIgnore());
        }
        // if (StringUtil.isNotEmpty(jobInfoPo.getNormParams())) {
        // queryWrapper.eq(JobInfoColumns.NORM_PARAMS.name(),
        // jobInfoPo.getNormParams());
        // }
        // if (StringUtil.isNotEmpty(jobInfoPo.getNormShardingParams())) {
        // queryWrapper.eq(JobInfoColumns.NORM_SHARDING_PARAMS.name(),
        // jobInfoPo.getNormShardingParams());
        // }
        // if (StringUtil.isNotEmpty(jobInfoPo.getThreadRegBean())) {
        // queryWrapper.eq(JobInfoColumns.THREAD_REG_BEAN.name(),
        // jobInfoPo.getThreadRegBean());
        // }
        // if (StringUtil.isNotEmpty(jobInfoPo.getThreadExeBean())) {
        // queryWrapper.eq(JobInfoColumns.THREAD_EXE_BEAN.name(),
        // jobInfoPo.getThreadExeBean());
        // }
        // if (StringUtil.isNotEmpty(jobInfoPo.getThreadCheckBean())) {
        // queryWrapper.eq(JobInfoColumns.THREAD_CHECK_BEAN.name(),
        // jobInfoPo.getThreadCheckBean());
        // }
        // if (jobInfoPo.getThreadConcurrentMax() != null) {
        // queryWrapper.eq(JobInfoColumns.THREAD_CONCURRENT_MAX.name(),
        // jobInfoPo.getThreadConcurrentMax());
        // }
        if (jobInfoPo.getCreateTime() != null) {
            queryWrapper.eq(JobInfoColumns.CREATE_TIME.name(), jobInfoPo.getCreateTime());
        }
        if (jobInfoPo.getVersion() != null) {
            queryWrapper.eq(JobInfoColumns.VERSION.name(), jobInfoPo.getVersion());
        }
        return queryWrapper;
    }

    @Override
    public JobInfoPo queryFirst(JobInfoPo jobInfoPo) {
        QueryWrapper<JobInfoPo> queryWrapper = Wrappers.query();
        if (jobInfoPo.getId() != null) {
            queryWrapper.eq(JobInfoColumns.ID.name(), jobInfoPo.getId());
        }
        if (StringUtil.isNotEmpty(jobInfoPo.getBizCode())) {
            queryWrapper.eq(JobInfoColumns.BIZ_CODE.name(), jobInfoPo.getBizCode());
        }
        if (StringUtil.isNotEmpty(jobInfoPo.getBatchCode())) {
            queryWrapper.eq(JobInfoColumns.BATCH_CODE.name(), jobInfoPo.getBatchCode());
        }
        if (StringUtil.isNotEmpty(jobInfoPo.getTaskCode())) {
            queryWrapper.eq(JobInfoColumns.TASK_CODE.name(), jobInfoPo.getTaskCode());
        }
        if (StringUtil.isNotEmpty(jobInfoPo.getJobCode())) {
            queryWrapper.eq(JobInfoColumns.JOB_CODE.name(), jobInfoPo.getJobCode());
        }
        // if (jobInfoPo.getOrders() != null) {
        // queryWrapper.eq(JobInfoColumns.ORDERS.name(), jobInfoPo.getOrders());
        // }
        if (StringUtil.isNotEmpty(jobInfoPo.getEnabled())) {
            queryWrapper.eq(JobInfoColumns.ENABLED.name(), jobInfoPo.getEnabled());
        }
        // if (StringUtil.isNotEmpty(jobInfoPo.getJobDesc())) {
        // queryWrapper.eq(JobInfoColumns.JOB_DESC.name(), jobInfoPo.getJobDesc());
        // }
        if (StringUtil.isNotEmpty(jobInfoPo.getFailIgnore())) {
            queryWrapper.eq(JobInfoColumns.FAIL_IGNORE.name(), jobInfoPo.getFailIgnore());
        }
        // if (StringUtil.isNotEmpty(jobInfoPo.getNormParams())) {
        // queryWrapper.eq(JobInfoColumns.NORM_PARAMS.name(),
        // jobInfoPo.getNormParams());
        // }
        // if (StringUtil.isNotEmpty(jobInfoPo.getNormShardingParams())) {
        // queryWrapper.eq(JobInfoColumns.NORM_SHARDING_PARAMS.name(),
        // jobInfoPo.getNormShardingParams());
        // }
        // if (StringUtil.isNotEmpty(jobInfoPo.getThreadRegBean())) {
        // queryWrapper.eq(JobInfoColumns.THREAD_REG_BEAN.name(),
        // jobInfoPo.getThreadRegBean());
        // }
        // if (StringUtil.isNotEmpty(jobInfoPo.getThreadExeBean())) {
        // queryWrapper.eq(JobInfoColumns.THREAD_EXE_BEAN.name(),
        // jobInfoPo.getThreadExeBean());
        // }
        // if (StringUtil.isNotEmpty(jobInfoPo.getThreadCheckBean())) {
        // queryWrapper.eq(JobInfoColumns.THREAD_CHECK_BEAN.name(),
        // jobInfoPo.getThreadCheckBean());
        // }
        // if (jobInfoPo.getThreadConcurrentMax() != null) {
        // queryWrapper.eq(JobInfoColumns.THREAD_CONCURRENT_MAX.name(),
        // jobInfoPo.getThreadConcurrentMax());
        // }
        if (jobInfoPo.getCreateTime() != null) {
            queryWrapper.eq(JobInfoColumns.CREATE_TIME.name(), jobInfoPo.getCreateTime());
        }
        if (jobInfoPo.getVersion() != null) {
            queryWrapper.eq(JobInfoColumns.VERSION.name(), jobInfoPo.getVersion());
        }
        // queryWrapper.orderBy(true, false, JobInfoPoColumns.ID.name());
        return super.getOne(queryWrapper);
    }

    @Override
    public JobInfoPo queryById(Serializable id) {
        return super.getById(id);
    }

    @Override
    public boolean insert(JobInfoPo jobInfoPo) {
        jobInfoPo.setVersion(1);
        jobInfoPo.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
        return super.save(jobInfoPo);
    }

    @Override
    public boolean insertBatch(List<JobInfoPo> listJobInfoPos) {
        int rs = getBaseMapper().insertBatchSomeColumn(listJobInfoPos);
        return SqlHelper.retBool(rs);
    }

    @Override
    public boolean updateById(JobInfoPo jobInfoPo) {
        UpdateWrapper<JobInfoPo> updateWrapper = Wrappers.update();
        updateWrapper.eq(JobInfoColumns.ID.name(), jobInfoPo.getId());
        updateWrapper.eq(JobInfoColumns.VERSION.name(), jobInfoPo.getVersion());
        jobInfoPo.setVersion(jobInfoPo.getVersion() + 1);
        boolean rs = super.update(jobInfoPo, updateWrapper);
        // 根据ID更新所有字段，空值也更新
        // int num = getBaseMapper().alwaysUpdateSomeColumnById(BatchInfoPo);
        // boolean rs = SqlHelper.retBool(num);
        if (!rs) {
            throw new UpdateZeroException();
        }
        return rs;
    }

    @Override
    public boolean deleteByIds(Serializable[] ids) {
        UpdateWrapper<JobInfoPo> updateWrapper = Wrappers.update();
        updateWrapper.in(JobInfoColumns.ID.name(), (Object[]) ids);
        return super.remove(updateWrapper);
    }

//    public JobInfoPo queryByExample(JobInfoPo jobInfoPo) {
//        QueryWrapper<JobInfoPo> queryWrapper = Wrappers.query();
//        if (jobInfoPo.getId() != null) {
//            queryWrapper.eq(JobInfoColumns.ID.name(), jobInfoPo.getId());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getBizCode())) {
//            queryWrapper.eq(JobInfoColumns.BIZ_CODE.name(), jobInfoPo.getBizCode());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getBatchCode())) {
//            queryWrapper.eq(JobInfoColumns.BATCH_CODE.name(), jobInfoPo.getBatchCode());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getTaskCode())) {
//            queryWrapper.eq(JobInfoColumns.TASK_CODE.name(), jobInfoPo.getTaskCode());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getJobCode())) {
//            queryWrapper.eq(JobInfoColumns.JOB_CODE.name(), jobInfoPo.getJobCode());
//        }
//        if (jobInfoPo.getOrders() != null) {
//            queryWrapper.eq(JobInfoColumns.ORDERS.name(), jobInfoPo.getOrders());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getEnabled())) {
//            queryWrapper.eq(JobInfoColumns.ENABLED.name(), jobInfoPo.getEnabled());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getJobDesc())) {
//            queryWrapper.eq(JobInfoColumns.JOB_DESC.name(), jobInfoPo.getJobDesc());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getFailIgnore())) {
//            queryWrapper.eq(JobInfoColumns.FAIL_IGNORE.name(), jobInfoPo.getFailIgnore());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getNormParams())) {
//            queryWrapper.eq(JobInfoColumns.NORM_PARAMS.name(), jobInfoPo.getNormParams());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getNormShardingParams())) {
//            queryWrapper.eq(JobInfoColumns.NORM_SHARDING_PARAMS.name(), jobInfoPo.getNormShardingParams());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getThreadRegBean())) {
//            queryWrapper.eq(JobInfoColumns.THREAD_REG_BEAN.name(), jobInfoPo.getThreadRegBean());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getThreadExeBean())) {
//            queryWrapper.eq(JobInfoColumns.THREAD_EXE_BEAN.name(), jobInfoPo.getThreadExeBean());
//        }
//        if (StringUtil.isNotEmpty(jobInfoPo.getThreadCheckBean())) {
//            queryWrapper.eq(JobInfoColumns.THREAD_CHECK_BEAN.name(), jobInfoPo.getThreadCheckBean());
//        }
//        if (jobInfoPo.getThreadConcurrentMax() != null) {
//            queryWrapper.eq(JobInfoColumns.THREAD_CONCURRENT_MAX.name(), jobInfoPo.getThreadConcurrentMax());
//        }
//        if (jobInfoPo.getAddTime() != null) {
//            queryWrapper.eq(JobInfoColumns.ADD_TIME.name(), jobInfoPo.getAddTime());
//        }
//        if (jobInfoPo.getVersion() != null) {
//            queryWrapper.eq(JobInfoColumns.VERSION.name(), jobInfoPo.getVersion());
//        }
//		Page<JobInfoPo> page = new Page<>(0, 1, false);
//		queryWrapper.orderBy(true, false, JobInfoPoColumns.ID.name());
//    }

}
